package com.ets.gui;
import com.ets.User_DAO.*;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.table.JTableHeader;

import com.ets.User_DAO.UserDB;

public class UserPanel extends JPanel 
  {
   private static final long serialVersionUID = 1;	
		
     private static UserDB          userDAO = null;	
     private static ArrayList<User> users;
     
	
   private JButton addButton,
				   changeButton,
				   exitButton;
   
   private JLabel userSummaryLabelOne,
   				  userSummaryLabelTwo;	
   	
	public UserPanel()
	{
		// User Display Label Panel
		JPanel userDisplayLabelPanel = new JPanel();
		userDisplayLabelPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		userSummaryLabelOne = new JLabel("User Summary");
		userSummaryLabelTwo = new JLabel(" ");
		userDisplayLabelPanel.add(userSummaryLabelOne);
		userDisplayLabelPanel.add(userSummaryLabelTwo);
		
		// User Display Panel
		setLayout(new GridBagLayout());
		
		Border loweredBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED);
		
		JPanel userDisplayPanel = new JPanel();
		
		userDisplayPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
 		userDAO = new UserDB();
 		//users = userDAO.getUsers();
 		users = new ArrayList<User>();
 		boolean flag = userDAO.getUsers(users);
 		System.out.println(flag);
 		// should be true;
 		

 		//Object[][] tempData = new Object[100][12];
 		Object[][] tempData = new Object[100][03];
 		Object[][] data     = tempData;
		
		// String col[] = {"User ID","Pswd","Nm_Pfx","First Name","Nm_Mid","Last Name","Nm_Sfx","Addr","Phn1","Phn2","Email","Email2"};
		String col[] = {"User ID","First Name","Last Name"};
		
		int m = 0;
		
 		for (User aa : users)
 		{
        	System.out.println(aa.getNmFst());
 			tempData[m][0] = aa.getUser();
    		tempData[m][1] = aa.getNmFst();
    	 	tempData[m][2] = aa.getNmLst();

/* 			
        	System.out.println(aa.getNmFst());
 			tempData[m][0] = aa.getUser();
    		tempData[m][1] = aa.getNmFst();
    	 	tempData[m][2] = aa.getNmMid();
    		tempData[m][3] = aa.getNmLst();
    	 	tempData[m][4] = aa.getPhn1();
    	 	tempData[m][5] = aa.getPhn2();
*/    	 	
    	 	
/*   		
     		System.out.println(tempData[m][0] + " " + tempData[m][1] + " " + tempData[m][2] + " " + tempData[m][3]);
*/     		
 		
    		m++;
  		}		
				
		JTable table = new JTable(data, col);
 		
		table.getColumnModel().getColumn(0).setPreferredWidth(100); 
		table.getColumnModel().getColumn(1).setPreferredWidth(150); 
		table.getColumnModel().getColumn(2).setPreferredWidth(250);
 
		
		
/*		
		table.getColumnModel().getColumn(4).setPreferredWidth(100); 
		table.getColumnModel().getColumn(5).setPreferredWidth(100);
*/		 

		table.setGridColor(Color.BLACK);
		JTableHeader header = table.getTableHeader();
		header.setBackground(Color.GRAY);
		table.setBackground(Color.white);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		
		JScrollPane pane = new JScrollPane(table);
		
		pane.setPreferredSize(new Dimension(500, 400)); 
		userDisplayPanel.add(pane);
		
		// button panel
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		
		//Edit button
		addButton = new JButton("  Edit ");
		buttonPanel.add(addButton);
		addButton.setToolTipText("Press to edit/change selected User...");
/*		
		//Change button
		changeButton = new JButton("Change User");
		buttonPanel.add(changeButton);
		changeButton.setToolTipText("This is the change button");
*/	
		
		//Delete button
		exitButton = new JButton(" Delete ");
		buttonPanel.add(exitButton);
		exitButton.setToolTipText("Press to delete selected User...");
		
		this.add(userDisplayLabelPanel, getConstraints(1,0,3,1, GridBagConstraints.CENTER)); 
	
		this.add(userDisplayPanel, getConstraints(1,1,3,1, GridBagConstraints.CENTER));
		
		this.add(buttonPanel, getConstraints(1,2,3,1, GridBagConstraints.CENTER));
	}
	
    private void setResizable(boolean b) {
		// TODO Auto-generated method stub
		
	}

	private GridBagConstraints getConstraints(int gridx, int gridy,
    	    int gridwidth, int gridheight, int anchor)
    	    {
    	        GridBagConstraints c = new GridBagConstraints();
    	        c.insets = new Insets(5, 5, 5, 5);
    	        c.ipadx = 5;
    	        c.gridx = gridx;
    	        c.gridy = gridy;
    	        c.gridwidth = gridwidth;
    	        c.gridheight = gridheight;
    	        c.anchor = anchor;
    	        return c;
    	    }
  }
